<?php
/*  ================================================================== *\
    (C) Copyright 2010 by Kenneth Downs

    This file is part of Triangulum 

    Triangulum is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Triangulum is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Triangulum; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor,
    Boston, MA  02110-1301  USA
    or visit http://www.gnu.org/licenses/gpl.html
\*  ================================================================== */
/**
 *  This file contains the 'getCurrent' or features for Postgres
 *  
 *  Quick Start: {@link main The main() method} is where to start.
 *
 *  There is no theory yet on whether the various getCurrent classes
 *  will inherit from a common base class.  Inasmuch as we can use
 *  "information_schema", we do not even need separate classes, so
 *  that may be possible.  We will just concentrate on postgres and
 *  see what happens when we start supporting more back ends.
 *
 *  The structure of the outputs is:
 *
 *  $this->tables = array(
 *      [tablename] = array(
 *           [columns] = array(
 *               [colname] = array(
 *                   [type]      = 
 *                   [precision] = 
 *                   [scale]     =
 *               )
 *           )
 *           [pk] = array(col1,col2,...)
 *           [fks] = array(
 *               [name] = string
 *              ,[cols] = array(
 *                    [childtable] => [partable]
 *               )
 *           )
 *           [indexes] = array(
 *               [name] = array(col1,col2,...)
 *               )
 *           )
 *      )
 *  )
 *  @author Kenneth Downs <kdowns@downsfam.net>
 *  @package Triangulum
 *
 */
class getCurrentPostgres {
    /**
     * The complete output
     *
     * @var String Output result
     */ 
    public $tables = array();

    /**
     * An array of errors
     *
     * @var Array any errors encountered
     */ 
    private $errors = array();
    
    
    /**
     * The getCurrent process begins with a call to main
     * 
     * This is the master method that opens a connection,
     * queries the system catalogs, and generates a picture
     * of the database
     */
    public function main($connString) {
        $dbConn = pg_connect($connString);
        
    
    
        # the first query is really about pulling columns,
        # the tables fall out of that 
        $sql="
select c.table_name,c.column_name
     , data_type
     , is_nullable
     , coalesce(c.character_maximum_length,c.numeric_precision) as precision
     , coalesce(c.numeric_scale)                                as scale
  from information_schema.tables t
  join information_schema.columns c ON t.table_name = c.table_name
 where t.table_schema = 'public'
   AND t.table_type   = 'BASE TABLE'
 order by c.table_name
        , c.column_name    ";
        $dbResults = pg_query($sql);
        $rows = pg_fetch_all($dbResults);
     
        foreach($rows as $row) {
            $this->tables[$row['table_name']]['columns'][$row['column_name']] = array(
                'type'      => $row['data_type']
               ,'precision' => $row['precision']
               ,'scale'     => $row['scale']
               ,'nullable'  => $row['is_nullable']
            );
        }
    }
            
        
}        
